/*
 * @Author: lp
 * @Date: 2022-04-21 14:35:47
 * @LastEditTime: 2022-04-21 14:45:23
 * @LastEditors: Please set LastEditors
 * @Description: vite 打包 gzip
 * @FilePath: \nien-vue3-admin\vite\plugins\compression.ts
 */
import type { PluginOption } from 'vite'
import compressPlugin from 'vite-plugin-compression'

export default function configCompressPlugin(
  compress: 'gzip' | 'brotli' | 'none',
  deleteOriginFile = false,
): PluginOption | PluginOption[] {
  const compressList = compress.split(',')

  const plugins: PluginOption[] = []

  if (compressList.includes('gzip')) {
    plugins.push(
      compressPlugin({
        ext: '.gz',
        deleteOriginFile,
      }),
    )
  }

  if (compressList.includes('brotli')) {
    plugins.push(
      compressPlugin({
        ext: '.br',
        algorithm: 'brotliCompress',
        deleteOriginFile,
      }),
    )
  }
  return plugins
}